<?php
namespace App\Common\ErrorCode;

use App\Common\Output\JsonOutput;

/**
 * 全局业务错误码类
 * Class ErrorCode
 * @package App\Common
 */
class ErrorCode implements ErrorCodeInterface
{
    const ERROR_SUCCESS = 200;
    const ERROR_UNDEFINED_CODE = 40004; //未定义的错误码
    const ERROR_JWT_TOKEN_INVALID = 10001; //令牌已失效
    const ERROR_JWT_UNABLE_DECODE = 10002;  //令牌无法解析即非法令牌
    const ERROR_JWT_TOKEN_OUT_ACTION = 10003; //令牌不可用

    const ERROR_TOKEN_INVALID = 30001; //未登陆
    const ERROR_PARAM = 30002; //参数错误

    const ERROR_LOGIN_ACCOUNT_OR_PASSWORD = 30003;//账号或密码错误
    const ERROR_USER_UNDEFINED = 30004;//用户不存在
    const ERROR_USER_NO_PERMISSION = 30005; //用户无权操作

    const ERROR_SERVER_UNKNOWN = 50001; //服务端未知错误

    private static $dict = [
        self::ERROR_SUCCESS => "Success",
        self::ERROR_UNDEFINED_CODE => "未定义的错误码",
        self::ERROR_JWT_TOKEN_INVALID => '令牌已失效',
        self::ERROR_JWT_UNABLE_DECODE => '非法令牌',
        self::ERROR_JWT_TOKEN_OUT_ACTION => '令牌不可用',
        self::ERROR_TOKEN_INVALID => '未登陆',
        self::ERROR_SERVER_UNKNOWN => '服务端未知错误',
        self::ERROR_PARAM => '参数错误',
        self::ERROR_LOGIN_ACCOUNT_OR_PASSWORD => '账号或密码错误',
        self::ERROR_USER_UNDEFINED => '用户不存在',
        self::ERROR_USER_NO_PERMISSION => '用户无权操作'
    ];

    /**
     * @param int $code
     * @return mixed
     */
    public static function codeToMessage(int $code)
    {
        if (!in_array($code, array_keys(self::$dict))) {
            JsonOutput::error(self::ERROR_UNDEFINED_CODE);
        }

        return self::$dict[$code];
    }
}